package com.atwl.school_system.service.serviceImpl;

import com.alibaba.druid.util.StringUtils;
import com.atwl.school_system.bean.Address;
import com.atwl.school_system.bean.OrderInfo;
import com.atwl.school_system.bean.ProInfo;
import com.atwl.school_system.bean.vo.OrderInfoVo;
import com.atwl.school_system.bean.vo.ProInfoVo;
import com.atwl.school_system.mapping.AddressDao;
import com.atwl.school_system.mapping.OrderDao;
import com.atwl.school_system.mapping.ProDao;
import com.atwl.school_system.service.OrderService;
import com.atwl.school_system.service.ProService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.*;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private ProDao proDao;

    @Autowired
    private ProServiceImpl proService;

    @Autowired
    private AddressDao addressDao;


    @Override
    public List<OrderInfoVo> getOrder(String uuid, String username, String phone, Integer status) {
        List<OrderInfoVo> orderInfoVos = new ArrayList<>();
        List<OrderInfo> orderInfoList = orderDao.getOrderByParam(uuid, username, phone, status);
        return GetOrderVo(orderInfoList, orderInfoVos);
    }

    @Override
    public List<OrderInfoVo> getOrderByUserId(String userId, Integer status) {
        List<OrderInfo> orderList = orderDao.getOrderByUserId(userId, status);
        List<OrderInfoVo> orderInfoVos = new ArrayList<>();
        return GetOrderVo(orderList, orderInfoVos);
    }

    @Override
    public void createOrderB(OrderInfo orderInfo) {
        String timeStamp = String.valueOf(System.currentTimeMillis()).substring(6, 12);
        Random random = new Random();
        String uuid = timeStamp + random.nextLong() + orderInfo.getPayPhone().substring(4, 8);
        orderInfo.setPayUuid(uuid);
        orderInfo.setIsPay(false);
        orderInfo.setScheduledTime(new Date());
        // 设置地址
        Address address = addressDao.getAddressById(orderInfo.getAddressId());
        orderInfo.setCity(address.getCity());
        orderInfo.setProvince(address.getProvince());

        List<ProInfoVo> proInfo = proService.getProInfo(orderInfo.getProId(), null, null);
        ProInfoVo proInfoVo = proInfo.get(0);
        orderInfo.setPayPrice(proInfoVo.getProPrice() * orderInfo.getPayCount());
        orderInfo.setCreateTime(new Date());
        orderDao.createOrder(orderInfo);
    }

    @Override
    public boolean updateOrder(OrderInfo orderInfo) {
        OrderInfo order = orderDao.getOrder(orderInfo.getPayUuid());
        order.setPayName(orderInfo.getPayName());
        order.setPayPhone(orderInfo.getPayPhone());
        order.setProId(orderInfo.getProId());
        order.setPayCount(orderInfo.getPayCount());
        order.setPayPrice(orderInfo.getPayPrice());
        order.setStatus(order.getStatus());
        order.setUpdateTime(new Date());
        orderDao.updateOrder(orderInfo);
        return true;
    }

    private List<OrderInfoVo> GetOrderVo(List<OrderInfo> orderList, List<OrderInfoVo> orderInfoVos) {
        for (OrderInfo orderInfo : orderList) {
            String[] proIds = orderInfo.getProId().split(",");
            List<ProInfoVo> proInfoVoList = new ArrayList<>();
            for (String proId : proIds) {
                List<ProInfo> proInfos = proDao.getProInfo(proId, "1", "ASC");
                List<ProInfoVo> proInfoVos = proService.getProInfoVos(proInfos);
                if (proInfoVos.size() < 1) continue;
                proInfoVoList.add(proInfoVos.get(0));
            }
            OrderInfoVo orderInfoVo = new OrderInfoVo();
            BeanUtils.copyProperties(orderInfo, orderInfoVo);
            orderInfoVo.setProInfoVoList(proInfoVoList);
            orderInfoVos.add(orderInfoVo);
        }
        return orderInfoVos;
    }
}
